<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.quotientintelligence.PMF" %>
<%@ page import="java.util.List" %>
<%@ page import="com.quotientintelligence.caftrak.shared.ConsumptionEvent" %>
<%@ page import="com.quotientintelligence.caftrak.shared.Substance" %>
<%@ page import="javax.jdo.PersistenceManager" %>


<html>
  <body>

<%
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    if (user != null) {
%>
<p>Hello, <%= user.getNickname() %>! (You can
<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out</a>.)</p>
<%
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try
		{
		
		    String query = "select from " + ConsumptionEvent.class.getName();
		    @SuppressWarnings("unchecked")
			List<ConsumptionEvent> drinks = (List<ConsumptionEvent>) pm.newQuery(query).execute();
		    if (drinks.isEmpty()) {
		%><p>You have not consumed any drinks!</p>
<%
		    } else {
%> <p> <%= user.getNickname() %> drank:</p>
<%	        	for (ConsumptionEvent d : drinks) 
		            if ( d.getSubstance() != null)
					{ 
					if( d.getUser().equals(user))
						{
							%> <blockquote> On <%= d.getDate() %>, <%= d.getUser() %>   drank 
					<%= d.getSubstance().Name %> which contains 
					<%= d.getSubstance().get_quantity() %> <%= d.getSubstance().get_quantUnits() %> of  
					<%= d.getSubstance().get_substanceType() %>
					</blockquote> <%
					     } 	
			        }
		    } // END ELSE (for has no brackets)
		} // END TRY
		finally
		{
			pm.close();
		}
     }//end user null
     else {
%>
<p>Hello!
<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign in</a>
to track your drinks over time.</p>
<%
    }   
%>

  </body>
</html>